package UnitTests;

import static org.junit.Assert.*;

import java.sql.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import CCRMServer.Controllers.UserVerify;
import Common.Entities.STATUS;
import Common.Entities.Employee;

public class UserVerifyTests {

	// class private attributes
	private static java.sql.Connection c;
	private Employee oritst;
	private char[] ch = {'1', '2', '3', '4', '5', '6'};
	
	@Before
	public void setUp() throws Exception {
		// setup DB record for testing
		c = CCRMServer.DBConnect.mySQLConnection("localhost", "ccrm", "root", "Braude");
		String[] flds = {"employeeID", "employeeName", "employeeBirthday", "employeePhone", "employeePosition", "employeeDepartment", "employeeStatus", "employeeUserName", "employeePassword"};
		String[] vals = {"217", "oritst", "1980-12-12", "03-1234567", "pos", "dpr", "1", "oritst", "123456"};
		CCRMServer.DBConnect.insertSingleRecord(c, "Employees", flds, vals);
		
		// setup entity for expected results
		oritst = new Employee ("217", "oritst", Date.valueOf("1980-12-12"), "03-1234567", "pos", "dpr", STATUS.ACTIVE, "oritst", ch);
	}

	@After
	public void tearDown() throws Exception {
		// clean-up after test - restore environment to setup values
		CCRMServer.DBConnect.DeleteRecords(c, "Employees", "employeeID = '217'");
		c.close();
	}

	@Test
	// test a successful login scenario
	public void testRegisterUserAllFieldsOK() {
		Employee emp = UserVerify.registerUser("oritst", ch);
		assertEquals(oritst.getID(), emp.getID());
		assertEquals(oritst.getName(), emp.getName());
		assertEquals(oritst.getBithday(), emp.getBithday());
		assertEquals(oritst.getPhone(), emp.getPhone());
		assertEquals(oritst.getPosition(), emp.getPosition());
		assertEquals(oritst.getDepartment(), emp.getDepartment());
		assertEquals(oritst.getStatus(), emp.getStatus());
		assertEquals(oritst.getUserName(), emp.getUserName());
		assertEquals(oritst.getPassword(), emp.getPassword());
	}
	
	@Test
	// test a bad user-name scenario
	public void testRegisterUserBadUserName() {
		String name = UserVerify.registerUser("xxx", ch).getName();	// no such user-name: "xxx"
		assertNull(name);
	}

	@Test
	// test a bad password scenario
	public void testRegisterUserBadPassword() {
		char[] ch1 = {'1','1','1','1','1'};	// correct password is "123456"
		Employee emp = UserVerify.registerUser("oritst", ch1);
		assertNotNull(emp.getUserName());
		assertNull(emp.getID());
	}
	
	@Test
	// test an un-active user try to login scenario
	public void testRegisterUserNotActive() throws Exception {
		// set user record to not-active (employeeStatus = 0)
		String[] fields = {"employeeStatus"};
		String[] values = {"0"};
		String[] keyName = {"employeeID"};
		String[] keyVal = {"217"};
		CCRMServer.DBConnect.updateSingleRecord(c, "Employees", fields, values, keyName, keyVal);
		
		Employee emp = UserVerify.registerUser("oritst", ch);
		assertEquals(STATUS.NOT_ACTIVE, emp.getStatus());
	}
	
	@Test
	// test a suspended user try to login scenario
	public void testRegisterUserSuspended() throws Exception {
		// set user record to not-active (employeeStatus = 2)
		String[] fields = {"employeeStatus"};
		String[] values = {"2"};
		String[] keyName = {"employeeID"};
		String[] keyVal = {"217"};
		CCRMServer.DBConnect.updateSingleRecord(c, "Employees", fields, values, keyName, keyVal);
		
		Employee emp = UserVerify.registerUser("oritst", ch);
		assertEquals(STATUS.SUSPENDED, emp.getStatus());
	}
}
